package com.lb.bs.client.serialize;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

/**
 * program: fianl
 * author: bsworld.xie
 * create: 2020-01-20 16:09
 * description:
 */
public class JdkSerialization implements SerializationInterface {
    private static final Logger logger = LoggerFactory.getLogger(JdkSerialization.class);

    @Override
    public String deserialize(byte[] bytes) throws Exception {
        if ((bytes == null || bytes.length == 0)) {
            return null;
        }
        try (ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);) {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
            Object readObject = objectInputStream.readObject();
            if (readObject == null) {
                return null;
            }
            return String.valueOf(readObject);
        } catch (Exception e) {
            logger.error("deserialize(), bytes:{}, error:", bytes, e);
            throw e;
        }
    }


    public String deserializeNew(byte[] bytes) {
        if ((bytes == null || bytes.length == 0)) {
            return null;
        }
        try (ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);) {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
            Object readObject = objectInputStream.readObject();


        } catch (Exception e) {
//            logger.error("deserialize(), bytes:{}, error:", bytes, e);
        }
        return null;
    }

}
